package com.tenglong.teacher.ui.setting.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.requestInfo.ModifyPassword;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import com.tenglong.teacher.ui.common.view.XEditText;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangePasswordActivity extends BaseActivity implements XEditText.DrawableRightListener {

    private XEditText oldPassword, newsPassword, newsPasswordAgain;
    private TextView btnConfirm;
    private boolean isOldPassword = false;
    private boolean isNewsPassword = false;
    private boolean isNewsPasswordAgain = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initView();
        setListeners();
        setState();
    }

    private void setListeners() {
        btnConfirm.setOnClickListener(this);
        oldPassword.setDrawableRightListener(this);
        newsPassword.setDrawableRightListener(this);
        newsPasswordAgain.setDrawableRightListener(this);
        oldPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isOldPassword = s.toString().length() >= 6;
                setState();
            }
        });
        newsPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isNewsPassword = s.toString().trim().length() >= 6;
                setState();
            }
        });
        newsPasswordAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isNewsPasswordAgain = s.toString().trim().length() >= 6;
                setState();
            }
        });
    }


    private void setState() {
        boolean isClick = isNewsPassword && isOldPassword && isNewsPasswordAgain;
        btnConfirm.setEnabled(isClick);
        btnConfirm.setClickable(isClick);
        btnConfirm.setBackgroundResource(isClick ? R.drawable.change_password_btn_press_shape : R.drawable.change_password_btn_no_press_shape);
    }


    private void initView() {
        setTitle("修改密码");
        oldPassword = findViewById(R.id.oldPassword);
        newsPassword = findViewById(R.id.newsPassword);
        newsPasswordAgain = findViewById(R.id.newsPasswordAgain);
        btnConfirm = findViewById(R.id.btn_confirm);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (oldPassword.getText().toString().trim().length() < 6) {
                    ToastUtils.showCenterToast("原始密码不能少于6位");
                    return;
                }
                if (newsPassword.getText().toString().trim().length() < 6) {
                    ToastUtils.showCenterToast("新密码不能少于6位");
                    return;
                }
                if (oldPassword.getText().toString().trim().equals(newsPassword.getText().toString().trim())) {
                    ToastUtils.showCenterToast("新密码不能与原始密码相同");
                    return;
                }
                if (!newsPasswordAgain.getText().toString().trim().equals(newsPassword.getText().toString().trim())) {
                    ToastUtils.showCenterToast("两次输入的新密码不一致");
                    return;
                }
                modifyPassword();
                break;
        }
    }

    private void modifyPassword() {
        showProgressDialog("密码修改中...");
        ModifyPassword modifyPassword = new ModifyPassword();
        modifyPassword.type = 1;
        modifyPassword.newsPassword = newsPassword.getText().toString().trim();
        modifyPassword.oldPassword = oldPassword.getText().toString().trim();
        CreateRequestEntity.getWebService().modifyPassword(modifyPassword).enqueue(new Callback<BaseResult<ClientUserInfo>>() {
            @Override
            public void onResponse(Call<BaseResult<ClientUserInfo>> call, Response<BaseResult<ClientUserInfo>> response) {
                dismissProgressDialog();
                if (ResultVerifier.isSucceed(response)) {
                    ToastUtils.showCenterToast("密码修改成功");
                    finish();
                } else {
                    ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                }
            }

            @Override
            public void onFailure(Call<BaseResult<ClientUserInfo>> call, Throwable t) {
                dismissProgressDialog();
                ResultVerifier.systerErrers(t);
            }
        });
    }

    @Override
    public void onDrawableRightClick(View view) {
        EditText editText = (EditText) view;
        int length = editText.getText().toString().length();
        if (TextUtils.isEmpty(editText.getText().toString())) {
            if (!editText.isSelected()) {
                ToastUtils.showCenterToast("请先输入密码");
                return;
            }
        }
        editText.setSelected(!editText.isSelected());
        if (editText.isSelected()) {
            //如果选中，显示密码
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //否则隐藏密码
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        editText.setSelection(length);
    }
}
